﻿using ManYan.Common;
using ManYan.Domain.DomainService;
using ManYan.Domain.Model.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ManYan.Application
{
    public class NavAppService : BaseAppService<tb_system_nav>, INavAppService
    {
        public NavAppService(IBaseDomainService<tb_system_nav> baseDomainService) : base(baseDomainService)
        {
        }

        public Res GetNavData()
        {
            var list = LoadEntityAll();
            return new Res() { Data= this.GetItems(list, Guid.Empty),Code=200,Msg="获取数据成功" };
        }
        /// <summary>
        /// 获取菜单数据
        /// </summary>
        /// <returns></returns>
    
        private object GetItems(List<tb_system_nav> data, Guid? pid)
        {
            List<object> itemData = new List<object>();
            var resData = data?.Where(x => x.PID == pid).ToList();
            resData?.ForEach(x =>
            {
                if (data?.Where(y => y.PID == x.Id).Count() > 0)
                {
                    itemData.Add(new
                    {

                        navName = x?.NavName,
                        navUrl = x?.NavUrl,
                        id=x?.Id,
                        children = GetItems(data, x?.Id)
                    });
                }
                else
                {
                    itemData.Add(new
                    {

                        navName = x?.NavName,
                        id=x?.Id,
                        navUrl = x?.NavUrl

                    });
                }
            });
            return itemData;

        }
    }
}
